<?php
/**
 * 文章数据模型
 * User: Lin Hui
 * Date: 2017/5/22
 * Time: 10:07
 */

namespace Mobile\Model;


use Think\Model;

class ArticleModel extends Model
{
    /**
     * 根据分类获取文章
     * @param int $website_id 站点ID
     * @param int $category_id 分类ID
     * @param int $size 条数
     * @return array
     */
    public function getArticleByCategory($website_id, $category_id, $size = 20)
    {
        $type_website = array_search('website', C('ARTICLE.attribute'));
        $type_category = array_search('category', C('ARTICLE.attribute'));
        $type_thumb = array_search('thumb', C('ARTICLE.attribute'));
        $attr_model = M('article_attributes');
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ website ON website.value = {$website_id} AND website.type = {$type_website} AND a.article_id = website.article_id
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        $total = $this->alias('a')
            ->join($join)
            ->count();
        $page = array(
            'nowPage'=>1,
            'totalPage'=>1,
        );
        $page['totalPage'] = ceil($total / $size) > $page['totalPage'] ? ceil($total / $size) : $page['totalPage'];
        $page['nowPage'] = I('p', 1, 'intval')  <= $page['totalPage'] ?  I('p', 1, 'intval') : $page['nowPage'];
        $list = $this->alias('a')
            ->join($join)
            ->order('sort, create_time DESC')
            ->page($page['nowPage'], $size)
            ->field('a.*')
            ->select();
        foreach($list as $k => $item) {
            $thumb =$attr_model->where(array('article_id'=>$item['article_id'], 'type'=>$type_thumb))->getField('value');

            $list[$k]['thumb'] = json_decode($thumb, true)[$website_id];
        }
        return array($list, $page);
    }

    /**
     * 获取文章信息
     * @param $article_id
     * @return bool|mixed
     */
    public function getInfo($article_id)
    {
        $article_id = intval($article_id);
        if($article_id) {
            return $this->find($article_id);
        } else{
            return false;
        }
    }

    /**
     * 获取上一篇（按发布时间为准）
     * @param $website_id
     * @param $category_id
     * @param $time
     * @return mixed
     */
    public function getUpArticle($website_id, $category_id, $time)
    {
        $type_website = array_search('website', C('ARTICLE.attribute'));
        $type_category = array_search('category', C('ARTICLE.attribute'));
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ website ON website.value = {$website_id} AND website.type = {$type_website} AND a.article_id = website.article_id
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        return $this->alias('a')
            ->join($join)
            ->order('sort, create_time DESC')
            ->where(array('create_time'=>array('lt', $time)))
            ->field('a.article_id, a.title')
            ->find();
    }

    /**
     * 获取下一篇（按发布时间为准）
     * @param $website_id
     * @param $category_id
     * @param $time
     * @return mixed
     */
    public function getDownArticle($website_id, $category_id, $time)
    {
        $type_website = array_search('website', C('ARTICLE.attribute'));
        $type_category = array_search('category', C('ARTICLE.attribute'));
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ website ON website.value = {$website_id} AND website.type = {$type_website} AND a.article_id = website.article_id
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        return $this->alias('a')
            ->join($join)
            ->order('sort, create_time')
            ->where(array('create_time'=>array('gt', $time)))
            ->field('a.article_id, a.title')
            ->find();
    }

    /**
     * 添加点击
     * @param $id
     * @return bool
     */
    public function addClick($id)
    {
        return $this->where(array('article_id'=>$id))
            ->save(array(
                'click'=>array('exp', 'click + 1')
            ));
    }
}